package com.blueocean_health.care.management.common.base.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


/**
 * ZIP压缩包
 * @author huyanqiu
 *
 */
public class ZipUtils {
	
	/**
	 * 解压zip
	 * @param fis  文件流
	 * @param path 目标路径
	 * @throws IOException 
	 */
	public static String decompressionZip(FileInputStream fis, String parent) throws IOException {
		String entryName = "";
		long startTime=System.currentTimeMillis();  
		//输入源zip路径  
        ZipInputStream zin = new ZipInputStream(fis, Charset.forName("GBK")); 
        BufferedInputStream bin = new BufferedInputStream(zin);  
        File fout = null;  
        try { 
        	// 实例化对象，指明要进行解压的文件
        	ZipEntry entry = null;//zin.getNextEntry();
        	// 获取下一个ZipEntry
            while(((entry = zin.getNextEntry())!=null && !entry.isDirectory()) || ((entry = zin.getNextEntry())!=null && !entry.isDirectory())){ 
            	entryName = entry.getName();
            	fout = new File(parent,entry.getName());  
                if(!fout.exists()){  
                    (new File(fout.getParent())).mkdirs();  
                }  
                FileOutputStream out=new FileOutputStream(fout);  
                BufferedOutputStream Bout=new BufferedOutputStream(out);  
                int b;  
                while((b=bin.read())!=-1){  
                    Bout.write(b);  
                }  
                Bout.close();  
                out.close();  
            }  
            bin.close();  
            zin.close();  
        } catch (IOException e) {  
           throw new IOException("解析失败");
        }  
        long endTime=System.currentTimeMillis();  
        System.out.println("耗费时间： "+(endTime-startTime)+" ms"); 
        return entryName;
	}
	
	public static void main(String[] args) throws Exception{
		String path = "D:/新建文件夹(2).zip";
		String d = "D:/";
		FileInputStream fileInputStream = new FileInputStream(new File(path));
		decompressionZip(fileInputStream, d);
	}
	
}
